package org.yunshang.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yunshang.customer.domain.TCustomer;
import org.yunshang.customer.domain.bo.TCustomerBo;
import org.yunshang.customer.domain.vo.TCustomerVo;
import org.yunshang.customer.mapper.TCustomerMapper;
import org.yunshang.customer.service.ITCustomerService;
import org.yunshang.common.core.utils.MapstructUtils;
import org.yunshang.common.core.utils.StringUtils;
import org.yunshang.common.excel.utils.ImageUtil;
import org.yunshang.common.mybatis.core.page.PageQuery;
import org.yunshang.common.mybatis.core.page.TableDataInfo;
import org.yunshang.system.domain.vo.SysOssVo;
import org.yunshang.system.mapper.SysOssMapper;

import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 客户信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-28
 */
@RequiredArgsConstructor
@Service
public class TCustomerServiceImpl implements ITCustomerService {

    private final TCustomerMapper baseMapper;

    private final SysOssMapper sysOssMapper;

    /**
     * 查询客户信息
     */
    @Override
    public TCustomerVo queryById(Long id){
        TCustomerVo customerVo = baseMapper.selectVoById(id);
        //获取oss图片信息
        return getLogUrl(customerVo);

    }
    public TCustomerVo getLogUrl(TCustomerVo customerVo){
        SysOssVo ossVo = sysOssMapper.selectVoById(customerVo.getLog());
        Optional<SysOssVo> optional = Optional.ofNullable(ossVo);;
        if (optional.isPresent()) {
            customerVo.setLogUrl(optional.get().getUrl());
            try {
                URL url = ImageUtil.downloadAndCompress(optional.get().getUrl(),0.1f);
                customerVo.setLogWebUrl(url);

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return customerVo;
    }


    /**
     * 查询客户信息列表
     */
    @Override
    public TableDataInfo<TCustomerVo> queryPageList(TCustomerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCustomer> lqw = buildQueryWrapper(bo);
        Page<TCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户信息列表
     */
    @Override
    public List<TCustomerVo> queryList(TCustomerBo bo) {
        LambdaQueryWrapper<TCustomer> lqw = buildQueryWrapper(bo);
        List<TCustomerVo> list = baseMapper.selectVoList(lqw);
        list.stream().forEach(e->{
            getLogUrl(e);

        });
        return list;
    }

    private LambdaQueryWrapper<TCustomer> buildQueryWrapper(TCustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCustomer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TCustomer::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getBigName()), TCustomer::getBigName, bo.getBigName());
        lqw.eq(StringUtils.isNotBlank(bo.getLeixing()), TCustomer::getLeixing, bo.getLeixing());
        lqw.eq(StringUtils.isNotBlank(bo.getPeople()), TCustomer::getPeople, bo.getPeople());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TCustomer::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), TCustomer::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), TCustomer::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), TCustomer::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getFacebook()), TCustomer::getFacebook, bo.getFacebook());
        lqw.eq(StringUtils.isNotBlank(bo.getCellAddress()), TCustomer::getCellAddress, bo.getCellAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLog()), TCustomer::getLog, bo.getLog());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TCustomer::getStatus, bo.getStatus());
        lqw.eq(bo.getBusinessUserId() != null, TCustomer::getBusinessUserId, bo.getBusinessUserId());
        lqw.eq(bo.getTax() != null, TCustomer::getTax, bo.getTax());
        lqw.eq(StringUtils.isNotBlank(bo.getFuliaoRemarks()), TCustomer::getFuliaoRemarks, bo.getFuliaoRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getPay()), TCustomer::getPay, bo.getPay());
        return lqw;
    }

    /**
     * 新增客户信息
     */
    @Override
    public Boolean insertByBo(TCustomerBo bo) {
        TCustomer add = MapstructUtils.convert(bo, TCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息
     */
    @Override
    public Boolean updateByBo(TCustomerBo bo) {
        TCustomer update = MapstructUtils.convert(bo, TCustomer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCustomer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean updateLog(Long cusId, Long ossId) {
        TCustomer customer =  new TCustomer();
        customer.setId(cusId);
        customer.setLog(String.valueOf(ossId));
        if (baseMapper.updateById(customer) > 0) {
            return true;
        }
        return false;
    }
}
